<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css"
      rel="stylesheet">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-table.min.css">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="../js/bootstrap-table.min.js"></script>
<script src="../js/bootstrap-table-zh-CN.min.js"></script>
<body>


<div class="modal fade m-auto" id="myModal">
    <div class="modal-dialog modal-xl" style="width: 1000px;height: 800px">
        <div class="modal-content">

            <!--     制作设计单模态框       -->
            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">生产工序设计单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <div class="m-auto row">
                    <!-- 父级编号 -->
                    <span id="parentId" style="display: none"></span>
                    <span id="moduleCostPriceSum1" style="display: none"></span>

                    <div class="col"><span>设计单编号:</span><span id="id"></span></div>
                    <div class="col"><span>设计人:</span><span id="designer"></span></div>
                </div>
                <div class="m-auto row" style="padding-top: 15px">
                    <div class="col"><span>产品名称:</span><span id="name"></span></div>
                    <div class="col"><span>产品编号:</span><span id="productId"></span></div>

                </div>
                <div style="padding-top: 15px">
                    <table id="tab1" class="table table-striped table-bordered table-hover">

                    </table>
                </div>
                <div class="row">
                    <div class="col"><span>工时成本:</span><span id="costPriceSum"></span></div>

                    <div class="col"></span><span>物料总成本:</span><span id="moduleSubtotal"></span></div>
                </div>

                <div style="padding-top: 15px">
                    <div><span>设计要求:</span><span id="procedureDescribe"></span></div>
                </div>

                <div class="m-auto row" style="padding-top: 15px">
                    <div class="col"><span>审核人:</span><span id="checker"></span></div>
                    <div class="col"><span>审核时间:</span><span id="checkTime"></span></div>
                </div>
                <div>
                    <div class="col"><span>审核意见:</span><span id="checkSuggestion"></span></div>
                </div>
            </div>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-warning" style="width: 90px;"
                        onclick="MDesignProcedureObj.BatchUpdateDesignProcedureProcess()">提交
                </button>
            </div>


        </div>
    </div>
</div>

<!-- 模态框 -->
<div class="modal fade" id="myModal1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">工序物料设计单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <div class="m-auto row">
                    <div class="col"><span>设计单编号:</span><span id="sheJi"></span></div>
                    <div class="col"><span>工序名称:</span><span id="gongXu"></span></div>
                </div>
                <div class="m-auto row" style="padding-top: 15px">
                    <div class="col"><span>登记人:</span><span id="register"></span></div>
                    <div class="col"><span>登记时间:</span><span id="registerTime"></span></div>

                </div>
            </div>

            <table id="tab2"></table>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary"  data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary"
                        onclick="MDesignProcedureObj.deleteMDesignProcedureModule()">变更
                </button>
            </div>

        </div>
    </div>
</div>

<!-- 模态框 -->
<div class="modal fade" id="myModal2">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title">工序物料设计单</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body">
                <div class="m-auto row">
                    <div class="col"><span>设计单编号:</span><span id="sheJi1"></span></div>
                    <div class="col"><span>工序名称:</span><span id="gongXu1"></span></div>
                </div>
                <div class="m-auto row" style="padding-top: 15px">
                    <div class="col"><span>登记人:</span><span id="register1"></span></div>
                    <div class="col"><span>登记时间:</span><span id="registerTime1"></span></div>

                </div>
            </div>

            <table id="tab3"></table>

            <!-- 模态框底部 -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="MDesignProcedureObj.AddDModuleDetailsText()">变更
                </button>
            </div>

        </div>
    </div>
</div>


<nav class="breadcrumb">
    <a class="breadcrumb-item" href="#">生产管理</a>
    <a class="breadcrumb-item" href="#">产品生产工序设计</a>
    <a class="breadcrumb-item" active href="#">产品生产工序设计单审核</a>
</nav>

<div class="input-group mb-3" style="width: 50%">
    <div class="input-group-prepend">
        <span class="input-group-text">设计编号</span>
    </div>

    <input id="designId" type="text" class="form-control">
    &nbsp;
    <div class="input-group-prepend">
        <span class="input-group-text">产品名称</span>
    </div>

    <input id="productName" type="text" class="form-control">
    &nbsp;
    <button onclick="$('#tab').bootstrapTable('refresh')" class="btn btn-primary">查询</button>
</div>

<table id="tab">

</table>
</body>


<script>
    $(function () {

        $("#tab").bootstrapTable({
            method: "post",
            url: "/QueryMDesignProcedureSuppliesAll.action",
            striped: true,
            pageNumber: 1,
            pagination: true,
            sidePagination: "server",
            height: 600,
            pageSize: 3,
            pageList: [3, 6, 9],
            queryParams: function (i) {
                return {
                    //计算当前页
                    // currentPage:(parts.offset/parts.limit)+1,
                    pageNumber: i.offset,
                    //每条页数
                    pageSize: i.limit,
                    designId: $("#designId").val(),
                    productName: $("#productName").val(),
                    checkTag: 'S001-1'
                }
            },
            columns: [
                {title: "设计单编号", field: "designId"},
                {title: "产品编号", field: "productId",},
                {title: "产品名称", field: "productName"},
                {title: "设计人", field: "designer"},
                {title: "登记时间", field: "registerTime"},
                {title: "工时总成本", field: "costPriceSum"},
                {
                    title: "变更", field: "id", align: "center",
                    formatter: function (value, row, index) {
                        return "<button onclick='MDesignProcedureObj.MDesignProcedureText(" + JSON.stringify(row) + ")' class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#myModal\">变更</button>";
                    }
                }

            ]
        })
    })
</script>

<script>
    var tab = 0;
    var labourHourAmount = 0;
    var costPrice = 0;
    var MDesignProcedureObj = {


        MDesignProcedureText: function (MDesignProcedure) {
            $('#tab1').bootstrapTable('destroy');

            $("#id").text(MDesignProcedure.designId)//设计单编号
            $("#sheJi").text(MDesignProcedure.designId)//设计单编号
            $("#designer").text(MDesignProcedure.designer)//设计人
            $("#name").text(MDesignProcedure.productName)//产品名称
            $("#productId").text(MDesignProcedure.productId)//产品编号
            $("#costPriceSum").text(MDesignProcedure.costPriceSum)//工时成本
            $("#checkTime").text(new Date().toLocaleString())//审核时间
            $("#procedureDescribe").text(MDesignProcedure.procedureDescribe)//设计要求
            $("#parentId").text(MDesignProcedure.id)//父级id
            $("#moduleSubtotal").text(MDesignProcedure.moduleCostPriceSum)//物料总成本
            $("#checker").text(MDesignProcedure.checker)//审核人
            $("#checkTime").text(MDesignProcedure.checkTime)//审核时间
            $("#checkSuggestion").text(MDesignProcedure.checkSuggestion)//审核意见
            console.log(JSON.stringify(MDesignProcedure))

            //获取当前等人账号
            $.ajax({
                type: "post",
                url: "/user.action",
                success: function (i) {
                    $("#person").text(i.loginId)//审核人
                }
            })

            $("#tab1").bootstrapTable({
                method: "get",
                url: "/queryMDesignProcedureDetailsByParentId.action",
                striped: true,
                queryParams: function (params) {
                    return {
                        id: MDesignProcedure.id
                    }
                },
                columns: [
                    {title: "序号", field: "id"},
                    {
                        title: "工序名称", field: "procedureName", formatter: function (value, row, index) {
                            $("#gongXu").text(value)
                            return "<span>" + value + "</span>"
                        }
                    },
                    {title: "工序编号", field: "procedureId"},
                    {title: "描述", field: "procedureDescribe"},
                    {title: "工时数", field: "labourHourAmount"},
                    {title: "单位(小时)", field: "amountUnit"},
                    {title: "工时成本小计(元)", field: "subtotal"},
                    {title: "物料成本小计(元)", field: "moduleSubtotal"},
                    {
                        title: "变更", field: "id", formatter: function (value, row, index) {
                            return " <button type=\"button\" class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#myModal1\" style=\"width: 90px;\" onclick=\"MDesignProcedureObj.AddText(" + value + ")\">变更</button>"
                        }
                    },

                ]
            })
        },
        AddMDesignProcedure: function (i) {
            var mDesignProcedureDetails = {
                parentId: $("#parentId").text(),
                procedureName: $(i).parent("td").siblings('.name').text(),
                procedureId: $(i).parent("td").siblings('.id').text()
            }

            $.ajax({
                type: "post",
                url: "/AddMDesignProcedureProcess.action",
                data: JSON.stringify(mDesignProcedureDetails),
                contentType: "application/json;charset=utf-8",
                success: function (value) {
                    if (value > 0) {
                        alert("添加成功");
                        $("#tab1").bootstrapTable("refresh")
                        $("#myModal2").modal("hide");
                    }
                }
            })

        },
        AddText: function (id) {
            this.Destroy();
            tab = id
            $("sheJi").text($("id").text())
            $.ajax({
                type: "post",
                url: "/user.action",
                success: function (i) {
                    $("#register").text(i.loginId)
                }
            })
            $("#registerTime").text(new Date().toLocaleString())

            $("#tab2").bootstrapTable({
                method: "get",
                url: "/queryMDesignProcedureModuleByParentId.action",
                striped: true,
                queryParams: function (params) {
                    return {
                        id: id
                    }
                },
                columns: [
                    {title: "序号", field: "id"},
                    {title: "物料名称", field: "productName"},
                    {title: "物料编号", field: "productId"},
                    {title: "描述", field: "productDescribe"},
                    {title: "本工序数量", field: "amount"},
                    {title: "单位", field: "amountUnit"},
                    {title: "单价(元)", field: "costPrice"},
                    {title: "小计(元)", field: "subtotal"}
                ]
            })
        },
        //变更删除之前数据
        deleteMDesignProcedureModule: function () {
            var MDesignProcedureModule = []
            $.each($("#tab2>tbody>tr"), function (i, v) {
                var a = {}
                var id = $("#tab2>tbody>tr").eq(i).children().eq(0).text()
                a.id = id
                MDesignProcedureModule.push(a)
            })
            console.log(MDesignProcedureModule)
            var parentId = $("#productId").text()
            if (confirm("变更将清除之前的数据")) {

                $.ajax({
                    type: "post",
                    url: "/deleteMDesignProcedureModule.action",
                    data: JSON.stringify(MDesignProcedureModule),
                    contentType: "application/json;charset=utf-8",
                    success: function (i) {
                        if (i > 0) {
                            $("#sheJi1").text($("#sheJi").text())
                            $("#gongXu1").text($("#gongXu").text())
                            $.ajax({
                                type: "post",
                                url: "/user.action",
                                success: function (i) {
                                    $("#register1").text(i.loginId)
                                }
                            })


                            $("#registerTime1").text(new Date().toLocaleString())

                            $.ajax({
                                type: "post",
                                url: "/queryDesignModuleByproductId.action",
                                data: "parenId=" + $("#productId").text(),
                                success: function (DesignModule) {
                                    $("#tab3").bootstrapTable({
                                        method: "get",
                                        url: "/queryDModuleDetailsByParentId.action",
                                        striped: true,
                                        queryParams: function (params) {
                                            return {
                                                parentId: DesignModule.id
                                            }
                                        }, columns: [
                                            {title: "序号", field: "id"},
                                            {title: "物料名称", field: "productName"},
                                            {title: "物料编号", field: "productId"},
                                            {title: "描述", field: "productDescribe"},
                                            {title: "设计数量", field: "amount"},
                                            {title: "可用数量", field: "residualAmount"},
                                            {title: "单位", field: "amountUnit"},
                                            {
                                                title: "单价(元)", field: "costPrice",
                                                formatter: function (value, row, index) {
                                                    return "<span>" + value + "</span>"
                                                }
                                            },
                                            {
                                                title: "本工序数量", formatter: function (value, row, index) {
                                                    return "<input  type='number' value='0'>"
                                                }
                                            },
                                        ]
                                    })
                                    $.ajax({
                                        type: "post",
                                        url: "/queryDesignModuleByproductId.action",
                                        data: "parenId=" + parentId,
                                        success: function (DesignModule) {
                                            console.log(DesignModule)
                                            var DModuleDetails = []
                                            $.each($("#tab2>tbody>tr"), function (i, v) {
                                                var a = {}
                                                var amount = $("#tab2>tbody>tr").eq(i).children().eq(4).text()
                                                var productName = $("#tab2>tbody>tr").eq(i).children().eq(1).text()
                                                a.parentId = DesignModule.id
                                                a.amount = amount
                                                a.productName = productName
                                                DModuleDetails.push(a)
                                            })
                                            console.log(DModuleDetails)
                                            $.ajax({
                                                type: "post",
                                                url: "/redesign.action",
                                                contentType: "application/json;charset=utf-8",
                                                data: JSON.stringify(DModuleDetails),
                                                success: function (i) {
                                                        $("#tab2").bootstrapTable("destroy")
                                                        $("#tab3").bootstrapTable("refresh")
                                                    console.log(11111)


                                                }
                                            })

                                        }
                                    })
                                }
                            })
                            $("#myModal2").modal("show")
                        }
                    }
                })
            }

        },

        AddDModuleDetailsText: function () {


            //获取值
            var result = []
            $.each($("#tab3>tbody>tr"), function (i, v) {
                var MDesignProcedureModule = {}
                var amount = $("#tab3>tbody>tr").eq(i).find("input[type='number']").val()//本工序数量
                var costPrice = $("#tab3>tbody>tr").eq(i).children().eq(7).text()//单价
                var parentId = $("#tab1>tbody>tr").eq(0).children().eq(0).text()//父级id
                var id = $("#tab3>tbody>tr").eq(i).children().eq(0).text()//id
                var productId = $("#tab3>tbody>tr").eq(i).children().eq(2).text()//物料编号
                var productName = $("#tab3>tbody>tr").eq(i).children().eq(1).text()//物料名称
                var productDescribe = $("#tab3>tbody>tr").eq(i).children().eq(3).text()//描述
                // var amountUnit=$("#tab3>tbody>tr").eq(i).children().eq(5).text()//可用数量
                var amountUnit = $("#tab3>tbody>tr").eq(i).children().eq(6).text()//单位
                var subtotal = costPrice * amount//小计
                var detailsNumber = i + 1

                MDesignProcedureModule.amount = amount
                MDesignProcedureModule.costPrice = costPrice
                MDesignProcedureModule.parentId = parentId
                MDesignProcedureModule.id = id
                MDesignProcedureModule.productId = productId
                MDesignProcedureModule.productName = productName
                MDesignProcedureModule.productDescribe = productDescribe
                MDesignProcedureModule.amountUnit = amountUnit
                MDesignProcedureModule.detailsNumber = detailsNumber
                MDesignProcedureModule.subtotal = subtotal


                result.push(MDesignProcedureModule)
            })

            console.log(JSON.stringify(result))

            //插入刚刚变更的数据
            $.ajax({
                type: "post",
                url: "/insertDModuleDetails.action",
                data: JSON.stringify(result),
                contentType: "application/json;charset=utf-8",
                success: function (i) {
                    if (i > 0) {
                        //计算物料成本小计添加进数据库
                        $.ajax({
                            type: "post",
                            url: "/queryMDesignProcedureModuleByParentId.action",
                            data: "id=" + $(tab)[0],
                            success: function (i) {
                                alert("变更成功")
                                var count = 0;
                                $.each(i, function (index, value) {
                                    count = i[index].subtotal
                                })
                                var a = {
                                    id: $(tab)[0],
                                    moduleSubtotal: count
                                }
                                console.log(a)
                                $.ajax({
                                    type: "post",
                                    url: "/updateMDesignProcedureDetailsModuleSubtotal.action",
                                    data: JSON.stringify(a),
                                    contentType: "application/json;charset=utf-8",
                                    success: function (i) {
                                        //计算物料总成本
                                        $.ajax({
                                            type: "post",
                                            url: "/queryMDesignProcedureDetailsByParentId.action",
                                            data: "id=" + $("#parentId").text(),
                                            success: function (list) {
                                                var count = 0;
                                                $.each(list, function (i, v) {
                                                    count += list[i].moduleSubtotal
                                                })
                                                var mDesignProcedure = {
                                                    id: $("#parentId").text(),
                                                    moduleCostPriceSum: count
                                                }
                                                console.log(mDesignProcedure)
                                                $.ajax({
                                                    type: "post",
                                                    url: "/updateMDesignProcedureModuleCostPriceSum.action",
                                                    data: JSON.stringify(mDesignProcedure),
                                                    contentType: "application/json;charset=utf-8",
                                                    success: function (i) {
                                                        if (i > 0) {
                                                            //变更成功减去本工序数量
                                                            $.ajax({
                                                                type: "post",
                                                                url: "/updateDModuleDetails.action",
                                                                data: JSON.stringify(result),
                                                                contentType: "application/json;charset=utf-8",
                                                                success: function (i) {
                                                                    // MDesignProcedureObj.MDesignProcedureText()
                                                                    // $("#tab1").modal("refresh")
                                                                    location.reload(true)
                                                                    $("#myModal3").modal("hide")
                                                                    $("#myModal2").modal("hide")
                                                                    $("#myModal1").modal("hide")

                                                                }
                                                            })
                                                        }
                                                    }
                                                })

                                            }

                                        })


                                    }

                                })
                            }
                        })


                    }


                }
            })

        },

        BatchUpdateDesignProcedureProcess: function () {

            var id = $("#id").text()
            $.ajax({
                type: "post",
                url: "/updateMDesignProcedureSuppliesTag.action",
                data: "designId=" + id,
                success: function (i) {
                    if (i > 0) {
                        alert("提交成功")
                        $("#myModal").modal("hide")
                        $("#tab").bootstrapTable("refresh")
                    }
                }
            })

        },
        Destroy:function (){
            $("#tab2").bootstrapTable("destroy")
        }
    }
</script>


</html>